<?php

namespace apiend\modules\statistic\models;

use Yii;
use common\helpers\ArrayHelper;
use yii\db\Query;
use yii\db\Expression;
use common\models\shop\ShopOrder;
use common\models\shop\ShopOrderGoods;
use common\models\shop\ShopOrderRefund;
use common\models\shop\ShopGoods;
use common\models\shop\ShopStore;

/**
 * 概况统计面板
 *
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class OverviewStatsChartSearch extends OverviewStatsChartModel {

    /**
     * @inheritdoc
     */
    public function getOrders() {
        $query = ShopOrder::find();
        $query->andFilterWhere([
            'store_id' => $this->store_id
        ]);
        $query->andDuration('created_at', $this->beginTime, $this->endTime);
        $query->select([
            'cnt' => 'count(*)',
            'gtime' => "FROM_UNIXTIME(created_at,'" . $this->dateFormat . "')"
        ]);
        $query->groupBy('gtime');
        $query->debug();
        $data = $query->asArray()->all();
        return [
            'name' => '下单量',
            'itemStyle' => $this->getItemStyle('primary'),
            'data' => $this->fillData($data),
        ];
    }

    /**
     * @inheritdoc
     */
    public function getDules() {
        $query = ShopOrder::find();
        $query->andFilterWhere([
            'store_id' => $this->store_id,
        ]);
        $query->andWhere([
            '>', 'pay_time', 0,
        ]);
        $query->andDuration('pay_time', $this->beginTime, $this->endTime);
        $query->select([
            'cnt' => 'count(*)',
            'gtime' => "FROM_UNIXTIME(pay_time,'" . $this->dateFormat . "')"
        ]);
        $query->groupBy('gtime');
        $query->debug();
        $data = $query->asArray()->all();
        return [
            'name' => '成交量',
            'itemStyle' => $this->getItemStyle('success'),
            'data' => $this->fillData($data),
        ];
    }

    /**
     * @inheritdoc
     */
    public function getServices() {
        $query = ShopOrderRefund::find();
        $query->andFilterWhere([
            'store_id' => $this->store_id,
        ]);
        $query->andWhere([
            '>', 'refund_time', 0,
        ]);
        $query->andDuration('refund_time', $this->beginTime, $this->endTime);
        $query->select([
            'cnt' => 'count(*)',
            'gtime' => "FROM_UNIXTIME(refund_time,'" . $this->dateFormat . "')"
        ]);
        $query->groupBy('gtime');
        $query->debug();
        $data = $query->asArray()->all();
        return [
            'name' => '退款量',
            'itemStyle' => $this->getItemStyle('danger'),
            'data' => $this->fillData($data),
        ];
    }

    /**
     * @inheritdoc
     */
    public function getItems() {
        return [
            $this->getOrders(),
            $this->getDules(),
            $this->getServices(),
        ];
    }

    /**
     * @inheritdoc
     */
    public function fields() {
        return [
            'labels' => 'groupDefaults',
            'items',
            'beginTime' => function() {
                return date('Y-m-d H:i:s', $this->beginTime);
            },
            'endTime' => function() {
                return date('Y-m-d H:i:s', $this->endTime);
            },
        ];
    }

}
